package com.zone.sdk.module.account;

import com.zone.sdk.common.http.base.BaseCloudServiceManager;
import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.sdk.module.account.entity.CaptchaCertExchangeEntity;
import com.zone.sdk.module.account.entity.CaptchaGetEntity;
import com.zone.sdk.module.account.entity.CheckCaptchaEntity;
import com.zone.sdk.module.account.entity.LoginEntity;
import com.zone.sdk.module.account.entity.LoginStatusEntity;
import com.zone.sdk.module.account.entity.QrLoginEntity;
import com.zone.sdk.module.account.entity.QrLoginPollEntity;
import com.zone.sdk.module.account.entity.UploadEntity;
import com.zone.sdk.module.account.entity.UserInfoEntity;
import com.zone.sdk.module.account.http.AccountOkHttpManager;
import com.zone.sdk.module.account.request.CaptchaCertExchangeRequest;
import com.zone.sdk.module.account.request.CaptchaGetRequest;
import com.zone.sdk.module.account.request.CheckCaptchaRequest;
import com.zone.sdk.module.account.request.LoginRequest;
import com.zone.sdk.module.account.request.LogoutRequest;
import com.zone.sdk.module.account.request.QrLoginPollRequest;
import com.zone.sdk.module.account.request.QrLoginRequest;
import com.zone.sdk.module.account.request.RefreshTokenRequest;
import com.zone.sdk.module.account.request.UploadRequest;
import com.zone.sdk.module.account.request.UserInfoRequest;
import com.zone.sdk.module.account.response.CaptchaGetResponse;

import org.jetbrains.annotations.NotNull;

import io.reactivex.observers.DisposableObserver;
import okhttp3.RequestBody;
import retrofit2.http.Body;
import retrofit2.http.Part;

public class CloudAccountServiceManager extends BaseCloudServiceManager {

    public static CloudAccountServiceManager instance;

    private final ICloudAccountService iCloudAccountService;

    private AccountOkHttpManager okHttpManager;

    private CloudAccountServiceManager(String baseUrl) {
        super();
        okHttpManager = AccountOkHttpManager.getInstance(baseUrl);
        iCloudAccountService = okHttpManager.build(ICloudAccountService.class);
    }

    public static CloudAccountServiceManager getInstance(String baseUrl) {
        if (instance == null){
            instance = new CloudAccountServiceManager(baseUrl);
            return instance;
        }
        return instance;
    }

    public void setAuthorization(String authorization){
        okHttpManager.setAuthorization(authorization);
    }

    public void setHeader(String key, String value){
        okHttpManager.setHeader(key, value);
    }


    /**********************************账号**********************************************/

    /**
     * 手机号+验证码登录/注册/忘记密码流程中，调用此接口获取短信验证码
     * @param request
     * @param callback
     */
    public void captchaGet(CaptchaGetRequest request, ResultCallBack<CaptchaGetResponse<CaptchaGetEntity>> callback){
        observableService.execute(iCloudAccountService.captchaGet(request) , new DisposableObserver<CaptchaGetResponse<CaptchaGetEntity>>(){

            @Override
            public void onNext(@NotNull CaptchaGetResponse<CaptchaGetEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 手机号+验证码登录/注册/忘记密码流程中，调用此接口获取短信验证码
     * @param request
     * @param callback
     */
    public void checkCaptcha(CheckCaptchaRequest request, ResultCallBack<CaptchaGetResponse<CheckCaptchaEntity>> callback){
        observableService.execute(iCloudAccountService.checkCaptcha(request) , new DisposableObserver<CaptchaGetResponse<CheckCaptchaEntity>>(){

            @Override
            public void onNext(@NotNull CaptchaGetResponse<CheckCaptchaEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 车机端同步下载队列应用并由云端推送到手机端
     * /user/captchacert/exchange/v1
     * @param request
     * @param callback
     */
    public void captchacertExchange(CaptchaCertExchangeRequest request, ResultCallBack<Result<CaptchaCertExchangeEntity>> callback){
        observableService.execute(iCloudAccountService.captchaCertExchange(request) , new DisposableObserver<Result<CaptchaCertExchangeEntity>>(){

            @Override
            public void onNext(@NotNull Result<CaptchaCertExchangeEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 手机号+验证码登录/注册/忘记密码流程中，调用此接口获取短信验证码
     * @param request
     * @param callback
     */
    public void loginPhoneCode(LoginRequest request, ResultCallBack<Result<LoginEntity>> callback){
        observableService.execute(iCloudAccountService.loginPhoneCode(request) , new DisposableObserver<Result<LoginEntity>>(){

            @Override
            public void onNext(@NotNull Result<LoginEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 手机号+验证码登录/注册/忘记密码流程中，调用此接口获取短信验证码
     * 与获取验证码接口相同，请求参数做区分 loginPhoneCode
     * @param request
     * @param callback
     */
    public void loginPhoneCodeCheck(LoginRequest request, ResultCallBack<Result<LoginEntity>> callback){
        observableService.execute(iCloudAccountService.loginPhoneCode(request) , new DisposableObserver<Result<LoginEntity>>(){

            @Override
            public void onNext(@NotNull Result<LoginEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 手机号+密码登录流程中，调用此接口提交登录请求，并获取登录信息
     * @param request
     * @param callback
     */
    public void loginPhonePwd(LoginRequest request, ResultCallBack<Result<LoginEntity>> callback){
        observableService.execute(iCloudAccountService.loginPhonePwd(request) , new DisposableObserver<Result<LoginEntity>>(){

            @Override
            public void onNext(@NotNull Result<LoginEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口设置密码。
     * @param request
     * @param callback
     */
    public void userPwdModify(LoginRequest request, ResultCallBack<Result> callback){
        observableService.execute(iCloudAccountService.userPwdModify(request) , new DisposableObserver<Result>(){

            @Override
            public void onNext(@NotNull Result o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口获取个人基本信息
     * @param request
     * @param callback
     */
    public void queryUserInfo(ResultCallBack<Result<UserInfoEntity>> callback){
        observableService.execute(iCloudAccountService.queryUserInfo() , new DisposableObserver<Result<UserInfoEntity>>(){

            @Override
            public void onNext(@NotNull Result<UserInfoEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口修改个人基本信息。
     * @param request
     * @param callback
     */
    public void updateUserInfo(UserInfoRequest request, ResultCallBack<Result> callback){
        observableService.execute(iCloudAccountService.updateUserInfo(request) , new DisposableObserver<Result>(){

            @Override
            public void onNext(@NotNull Result o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口退出当前账号
     * @param request
     * @param callback
     */
    public void logout(LogoutRequest request, ResultCallBack<Result> callback){
        observableService.execute(iCloudAccountService.logout(request) , new DisposableObserver<Result>(){

            @Override
            public void onNext(@NotNull Result o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 用户Access Token 过期状态下，调用此接口以获取新的Token。
     * @param request
     * @param callback
     */
    public void refreshToken(RefreshTokenRequest request, ResultCallBack<Result<LoginEntity>> callback){
        observableService.execute(iCloudAccountService.refreshToken(request) , new DisposableObserver<Result<LoginEntity>>(){

            @Override
            public void onNext(@NotNull Result<LoginEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口上传文件（移动端用于上传自定义头像）。
     * @param request
     * @param callback
     */
    public void userUpload(@Part("files") RequestBody body, @Body UploadRequest request, ResultCallBack<Result<UploadEntity>> callback){
        observableService.execute(iCloudAccountService.userUpload(body, request), new DisposableObserver<Result<UploadEntity>>(){

            @Override
            public void onNext(@NotNull Result<UploadEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口查询当前账户在所有设备上的登录状态
     * @param request
     * @param callback
     */
    public void loginStatus( ResultCallBack<Result<LoginStatusEntity>> callback){
        observableService.execute(iCloudAccountService.loginStatus(), new DisposableObserver<Result<LoginStatusEntity>>(){

            @Override
            public void onNext(@NotNull Result<LoginStatusEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 车机端调用此接口生成登录二维码，返回的是QR Code URL
     * @param request
     * @param callback
     */
    public void qrLoginCode(QrLoginRequest request, ResultCallBack<Result<QrLoginEntity>> callback){
        observableService.execute(iCloudAccountService.qrLoginCode(request), new DisposableObserver<Result<QrLoginEntity>>(){

            @Override
            public void onNext(@NotNull Result<QrLoginEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 车机端通过此接口轮询登录状态。
     * @param request
     * @param callback
     */
    public void qrLoginPolling(QrLoginPollRequest request, ResultCallBack<Result<QrLoginPollEntity>> callback){
        observableService.execute(iCloudAccountService.qrLoginPolling(request), new DisposableObserver<Result<QrLoginPollEntity>>(){

            @Override
            public void onNext(@NotNull Result<QrLoginPollEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }
    

    /*********************************************************************************/


}
